from rest_framework import serializers
from apps.users.models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email', 'mobile', 'password']
        extra_kwargs = {
            'password': {
                'write_only': True,
                'max_length': 20,
                'min_length': 5
            },
            'usrname': {
                'max_length': 20,
                'min_length': 5
            }
        }

    def create(self, validated_data):
        # **validated_data 就是解包操作
        return User.objects.create_user(**validated_data)


# class UserSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         fields = ['id', 'username', 'email', 'mobile', 'password']
#         extra_kwargs = {
#             'password': {
#                 'write_only': True,
#                 'max_length': 20,
#                 'min_length': 5
#             },
#             'username': {
#                 'max_length': 20,
#                 'min_length': 5
#             }
#         }
#
#     def create(self, validated_data):
#         return User.objects.create(**validated_data)